define(
["osm/OsmInfoSyst"],
function(OsmInfoSyst){
    return {
        getName: function() {
            return "SYSTEM - QUERY VIEW";
        },
        
        create: function(systemMgr, cam) {
            var _osmInfoSyst;
            var _comps;
            var _cam = cam;
            var _CLASS = this;

            function _getClass() { 
                return _CLASS;
            }

            function _hideAllViews() {
                $("#qv_featureTitle").hide();
                $("#qv_indoor").hide();
                $("#qv_list_container").hide();
                $("#qv_searchResult_container").hide();
            }

            function _setFeatureTitle(name, amenity) {
                //Validate params
                name = !name ? "No name" : name;
                amenity = !amenity ? "No type" : amenity

                //Show
                $("#qv_featureTitle").text(name + " (" + amenity + ")");
                $("#qv_featureTitle").show();
            }

            function _setIndoorOptions(
                curFloor, maxFloor, indoorActive,
                onFloorChanged, onIndoorToggled) {

                //Show the indoor view
                $("#qv_indoor").show();

                //Set values
                $("#qv_indoor_toggle").text(indoorActive ? "ON" : "OFF");
                $("#qv_indoor_curFloor").val(curFloor);
                $("#qv_indoor_maxFloor").text(maxFloor);

                //Set listeners, unbind previous listeners
                $("#qv_indoor_curFloor").unbind('keyup');
                $("#qv_indoor_curFloor").keyup(function(e){
                    var val = +$(this).val();
                    var valid = !isNaN(val) && val >= 0 && val <= maxFloor;
                    if(valid)
                        onFloorChanged(val);
                });
                $("#qv_indoor_toggle").unbind('click');
                $("#qv_indoor_toggle").click(function(e){
                    onIndoorToggled();
                    indoorActive = !indoorActive;
                    var txt = $("#qv_indoor_toggle").text();
                    $("#qv_indoor_toggle").text(indoorActive ? "ON" : "OFF");
                });
            }

            /**
             * tags : array of [name => value]
             */
            function _showTags(tags) {
                //Show the view
                $("#qv_list_container").show();

                //Build info
                var info = "";
                for(var name in tags) {
                    info += "<li>" + name + " : " + tags[name] + "</li>";
                }

                //Change list content
                $("#qv_list").text('');
                $("#qv_list").append(info);
            }

            function _showSearchResult(res) {
                //Modify title
                $("#qv_searchResult_title").text('Showing ' + res.length + ' result');

                //Push result
                var results = "";
                res.forEach(function(val){
                    results += '<li><a href="#" entId="' + val.id + '">' + val.name + " : " + val.amenity + "</a></li>";
                });
                $("#qv_searchResult_list").text('');
                $("#qv_searchResult_list").append(results);

                //Assign click listener
                $("#qv_searchResult_list li a").click(function(){ 
                    //Get id
                    var id = $(this).attr('entId'); 

                    //Select that entity
                    _selectMapEntity(id);
                });

                //Show
                $("#qv_searchResult_container").show();
            }

            function _selectMapEntity(id) {
                _getComp(id).select();
            }

            function _search(q) {
                //Hide views
                _hideAllViews();

                //Do the query
                var res = _osmInfoSyst.search(q);

                //Show result
                _showSearchResult(res);
            }

            function _init() {
                //Assign members
                _osmInfoSyst = systemMgr.getSystem(OsmInfoSyst);
                _comps = [];

                //Hide views
                _hideAllViews();

                //Register search function
                $("#qv_search_btn").click(function(){
                    var q = $("#qv_search_q").val();
                    _search(q);
                });
            }

            function _destroy() {
                //TODO: Unregister search
            }

            function _addComp(comp) {
                _comps["#" + comp.getOsmId()] = comp;
            }

            function _remComp(comp) {
                _comps["#" + comp.getOsmId()] = null;
            }

            function _getComp(id) {
                return _comps["#" + id];
            }

            function _setCam(cam) {
                _cam = cam;
            }

            function _getCam() {
                return _cam;
            }

            var _self = {
                getClass        : _getClass,
                init            : _init,
                destroy         : _destroy,
                addComp         : _addComp,
                remComp         : _remComp,
                hideAllViews    : _hideAllViews,
                setFeatureTitle : _setFeatureTitle,
                setIndoorOptions: _setIndoorOptions,
                showTags        : _showTags,
                setCam          : _setCam,
                getCam          : _getCam,
            }

            //Register to system
            systemMgr.addSystem(_self);

            return _self;
        }
    };
});